<?php

/**
 *Simple CRUD Code Generator
 *Email:  support@crazedcoders.com
 *Website: http://www.crazedcoders.com/
 *Copyright (C) 2007 CrazedCoders
 *
 *This program is free software: you can redistribute it and/or modify
 *it under the terms of the GNU General Public License as published by
 *the Free Software Foundation, either version 3 of the License, or
 *(at your option) any later version.
 *
 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * generates a remoting-config.xml file for flex
 * pre: $tables is already populated with key information
 */
function generateRemotingConfig( $tables ) {
	$relativeFlexRootPath = con::$relativeFlexRootPath;
	$filePath = "$relativeFlexRootPath/remoting-config.mxml";
	$includePath = "$relativeFlexRootPath/remoting-config.include";
	$export =
'<?xml version="1.0" encoding="utf-8" ?>
<service id="amfphp-flashremoting-service" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage">

   <default-channels>
      <channel ref="my-amfphp"/>
   </default-channels>
';

	foreach( $tables as $tablename=>$columns ) {
		$export .=
'
   <destination id="'.toCamelCase( $tablename ).'Dao">
      <properties>
         <source>*</source>
      </properties>
   </destination>

';

	}

#	<mx:RemoteObject id="RolesDaoRO"
#		destination="RolesDao"
#		source="com.crazedcoders.loginscreen.RolesDao"
#		fault="event.token.faultHandler(event)"
#		result="event.token.resultHandler(event)"
#		showBusyCursor="true" />
#
#	<mx:RemoteObject id="ForumDAO"
#		destination="ForumDAO"
#		source="com.crazedcoders.loginscreen.ForumDAO"
#		fault="event.token.faultHandler(event)"
#		result="event.token.resultHandler(event)"
#		showBusyCursor="true" />


	$include = "";
	if( file_exists( $includePath ) ) {
		$fh = fopen( $includePath );
		if( $fh ) {
			while( ! feof( $fh ) ) {
				$include .= fread( $fh, 8192 );
			}
			fclose( $fh );
		}
	}
	else {
		$include .=
'	<!-- no custom code .include found -->
';
	}
	$export .= $include;

	$export .=
'
	<!-- end user included code -->


</service>';
	return( $export );
}


?>
